package com.warm.pump.executor.batch;

import com.warm.pump.frame.util.SpringBeanUtil;
import com.warm.pump.module.skd.bean.po.gen.ScheduleInstanceDtl;
import com.warm.pump.module.skd.bean.po.gen.ScheduleLog;
import com.warm.pump.module.skd.service.IScheduleLogService;
import com.warm.pump.module.skd.service.impl.ScheduleLogServiceImpl;
import com.warm.pump.module.work.bean.po.gen.WorkItem;
import org.springframework.batch.core.ItemWriteListener;
import org.springframework.batch.core.annotation.AfterWrite;
import org.springframework.batch.core.annotation.BeforeWrite;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by guozj on 2018/3/1.
 */
public class CursorWriterLister implements ItemWriteListener {
    private ScheduleInstanceDtl instanceDtl;
    private WorkItem itemDtl;
    private IScheduleLogService logService = SpringBeanUtil.getBean(ScheduleLogServiceImpl.class);


    @Override
    @BeforeWrite
    public void beforeWrite(List list) {
    }

    @Override
    @AfterWrite
    public void afterWrite(List list) {
        new Thread(() -> {
            Map<String, Object> obMap = new HashMap<>();
            obMap.put("instanceId", instanceDtl.getInstanceId());
            obMap.put("instanceTaskId", instanceDtl.getId());
            obMap.put("workId", instanceDtl.getWorkId());
            obMap.put("workItemId", itemDtl.getId());
            ScheduleLog scheduleLog = logService.queryObject(obMap);

            ScheduleLog updateLog = new ScheduleLog();
            updateLog.setId(scheduleLog.getId());
            updateLog.setUpdateTime(new Date());
            updateLog.setDealCnt(scheduleLog.getDealCnt() + list.size());
            logService.update(updateLog);
        }).start();
    }

    @Override
    public void onWriteError(Exception e, List list) {
        new Thread(() -> {
            Map<String, Object> obMap = new HashMap<>();
            obMap.put("instanceId", instanceDtl.getInstanceId());
            obMap.put("instanceTaskId", instanceDtl.getId());
            obMap.put("workId", instanceDtl.getWorkId());
            obMap.put("workItemId", itemDtl.getId());
            ScheduleLog scheduleLog = logService.queryObject(obMap);
            ScheduleLog updateLog = new ScheduleLog();
            updateLog.setId(scheduleLog.getId());
            updateLog.setUpdateTime(new Date());
            updateLog.setStatus(2);
            updateLog.setError(createMessageContent(e));
            logService.update(updateLog);
        }).start();
    }

    private String createMessageContent(Exception e) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(stringWriter);
        e.printStackTrace(writer);
        StringBuffer buffer = stringWriter.getBuffer();
        return buffer.toString();
    }

    public void setInstanceDtl(ScheduleInstanceDtl instanceDtl) {
        this.instanceDtl = instanceDtl;
    }

    public void setItemDtl(WorkItem itemDtl) {
        this.itemDtl = itemDtl;
    }
}
